import {
  BaiduOutlined,
  BilibiliOutlined,
  GithubOutlined,
  GoogleOutlined,
  WeiboOutlined,
  ZhihuOutlined
} from '@ant-design/icons'
import useEngineStore from './../stores/engine'

export interface SearchConfig {
  label: string
  icon: JSX.Element
  value: string
  url: string
}

const useSearch = () => {
  const { engine } = useEngineStore()
  const searchConfig: Record<EngineType, SearchConfig> = {
    baidu: {
      label: '百度',
      icon: <BaiduOutlined />,
      value: 'baidu',
      url: 'https://www.baidu.com/s?wd='
    },
    zhihu: {
      label: '知乎',
      icon: <ZhihuOutlined />,
      value: 'zhihu',
      url: 'https://www.zhihu.com/search?type=content&q='
    },
    google: {
      label: '谷歌',
      icon: <GoogleOutlined />,
      value: 'google',
      url: 'https://www.google.com/search?q='
    },
    bilibili: {
      label: 'B站',
      icon: <BilibiliOutlined />,
      value: 'bilibili',
      url: 'https://search.bilibili.com/all?keyword='
    },
    weibo: {
      label: '微博',
      icon: <WeiboOutlined />,
      value: 'weibo',
      url: 'https://s.weibo.com/weibo?q='
    },
    github: {
      label: 'Git',
      icon: <GithubOutlined />,
      value: 'github',
      url: 'https://github.com/search?q='
    }
  }

  const handleStartSearch = (query: string) => {
    // window.electron.ipcRenderer.send('openBrower', `https://www.baidu.com/s?wd=${query}`)
    window.electron.ipcRenderer.send('openBrower', searchConfig[engine].url + query)
  }

  const getSuggestions = (query: string) => {
    return new Promise<any>((resolve, reject) => {
      window.electron.ipcRenderer
        .invoke('baidu', query)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          reject(err)
        })
    })
  }
  return {
    searchConfig,
    handleStartSearch,
    getSuggestions
  }
}

export default useSearch
